/*
 * @Description:
 * @Author: fhw
 * @Date: 2022-12-13 11:26:34
 */

import { JwtService } from "@nestjs/jwt";
import { RoleEntity } from "src/modules/role/entities/role.entity";
import { throwHttp } from "src/utlis";
import { isTrue } from "src/utlis/tools";

// src/modules/auth/constants.ts
export const jwtConstants = {
  secret: 'loveKey_245815',
};
export const signOptions = { expiresIn: '12h' }
export enum LoginDoType {
  User = 'user',
  Admin = 'admin'
}
export class TokenDto {
  public readonly userId: number;
  public readonly username: string;
  public readonly roles: RoleEntity;
  public readonly email: string
  public readonly type: LoginDoType
}

export function getTokenInfo(jwtService: JwtService, headers): TokenDto {
  const token = headers.authorization.split(' ');
  if (!isTrue(token)) {
    throwHttp('获取token失败');
    return
  }
  return this.jwtService.decode(token[1]) as TokenDto;
}